API: Changing all modules' getParamDescription(), getAllowedParams() and getDescripti...
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2
3 /*
4 * Created on Sep 1, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
28 }
29
30 /**
31 * @addtogroup API
32 */
33 class ApiProtect extends ApiBase {
34
35 public function __construct($main, $action) {
36 parent :: __construct($main, $action);
37 }
38
39 public function execute() {
40 global $wgUser;
41 $this->getMain()->requestWriteMode();
42 $params = $this->extractRequestParams();
43
44 $titleObj = NULL;
45 if(!isset($params['title']))
46 $this->dieUsageMsg(array('missingparam', 'title'));
47 if(!isset($params['token']))
48 $this->dieUsageMsg(array('missingparam', 'token'));
49 if(!isset($params['protections']) || empty($params['protections']))
50 $this->dieUsageMsg(array('missingparam', 'protections'));
51
52 if(!$wgUser->matchEditToken($params['token']))
53 $this->dieUsageMsg(array('sessionfailure'));
54
55 $titleObj = Title::newFromText($params['title']);
56 if(!$titleObj)
57 $this->dieUsageMsg(array('invalidtitle', $params['title']));
58
59 $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
60 if(!empty($errors))
61 // We don't care about multiple errors, just report one of them
62 $this->dieUsageMsg(current($errors));
63
64 if(in_array($params['expiry'], array('infinite', 'indefinite', 'never')))
65 $expiry = Block::infinity();
66 else
67 {
68 $expiry = strtotime($params['expiry']);
69 if($expiry < 0 || $expiry == false)
70 $this->dieUsageMsg(array('invalidexpiry'));
71
72 $expiry = wfTimestamp(TS_MW, $expiry);
73 if($expiry < wfTimestampNow())
74 $this->dieUsageMsg(array('pastexpiry'));
75 }
76
77 $protections = array();
78 foreach($params['protections'] as $prot)
79 {
80 $p = explode('=', $prot);
81 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
82 if($titleObj->exists() && $p[0] == 'create')
83 $this->dieUsageMsg(array('create-titleexists'));
84 if(!$titleObj->exists() && $p[0] != 'create')
85 $this->dieUsageMsg(array('missingtitles-createonly'));
86 }
87
88 $dbw = wfGetDb(DB_MASTER);
89 $dbw->begin();
90 if($titleObj->exists()) {
91 $articleObj = new Article($titleObj);
92 $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry);
93 } else
94 $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiry);
95 if(!$ok)
96 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
97 // Just throw an unknown error in this case, as it's very likely to be a race condition
98 $this->dieUsageMsg(array());
99 $dbw->commit();
100 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
101 if($expiry == Block::infinity())
102 $res['expiry'] = 'infinity';
103 else
104 $res['expiry'] = wfTimestamp(TS_ISO_8601, $expiry);
105
106 if($params['cascade'])
107 $res['cascade'] = '';
108 $res['protections'] = $protections;
109 $this->getResult()->addValue(null, $this->getModuleName(), $res);
110 }
111
112 public function mustBePosted() { return true; }
113
114 public function getAllowedParams() {
115 return array (
116 'title' => null,
117 'token' => null,
118 'protections' => array(
119 ApiBase :: PARAM_ISMULTI => true
120 ),
121 'expiry' => 'infinite',
122 'reason' => '',
123 'cascade' => false
124 );
125 }
126
127 public function getParamDescription() {
128 return array (
129 'title' => 'Title of the page you want to restore.',
130 'token' => 'A protect token previously retrieved through prop=info',
131 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
132 'expiry' => 'Expiry timestamp. If set to \'infinite\', \'indefinite\' or \'never\', the protection will never expire.',
133 'reason' => 'Reason for (un)protecting (optional)',
134 'cascade' => 'Enable cascading protection (i.e. protect pages included in this page)'
135 );
136 }
137
138 public function getDescription() {
139 return array(
140 'Change the protection level of a page.'
141 );
142 }
143
144 protected function getExamples() {
145 return array (
146 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000',
147 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
148 );
149 }
150
151 public function getVersion() {
152 return __CLASS__ . ': $Id$';
153 }
154 }